package org.xx.armory.spring5.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

import static org.xx.armory.commons.LocalDateUtils.minDate;
import static org.xx.armory.commons.Validators.rejectIfBlank;
import static org.xx.armory.commons.Validators.rejectIfNegative;
import static org.xx.armory.commons.Validators.rejectIfNull;

public final class Book {
    public static final String[] ALL_FIELD_NAMES = new String[]{"BOOK_ID", "TITLE", "AUTHOR", "PUB_DATE", "PRICE", "AMOUNT"};

    private final long id;
    private final String title;
    private final String author;
    private final Date pubDate;
    private final BigDecimal price;
    private final int amount;

    public Book(
            long id
    ) {
        this(id, "", "", minDate(), BigDecimal.ZERO, 1);
    }

    public Book(
            long id,
            String title,
            String author,
            Date pubDate,
            BigDecimal price,
            int amount
    ) {
        rejectIfNegative(id, "id");
        rejectIfBlank(title, "title");
        rejectIfBlank(author, "author");
        rejectIfNull(pubDate, "pubDate");
        rejectIfNull(price, "price");
        rejectIfNegative(amount, "amount");

        this.id = id;
        this.title = title;
        this.author = author;
        this.pubDate = pubDate;
        this.price = price;
        this.amount = amount;
    }

    public final long getId() {
        return this.id;
    }

    public final String getTitle() {
        return this.title;
    }

    public final String getAuthor() {
        return this.author;
    }

    public final Date getPubDate() {
        return this.pubDate;
    }

    public final BigDecimal getPrice() {
        return this.price;
    }

    public final int getAmount() {
        return this.amount;
    }

    @Override
    public int hashCode() {
        return (int) this.id;
    }

    @Override
    public boolean equals(
            Object obj
    ) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Book)) {
            return false;
        }
        final var other = (Book) obj;
        return this.id == other.id
                && Objects.equals(this.title, other.title) && Objects.equals(this.author, other.author) && Objects.equals(this.pubDate, other.pubDate) && Objects.equals(this.price, other.price);
    }

    @Override
    public String toString() {
        return "Book(" +
                "id=" + this.id +
                ",title=" + this.title +
                ",author=" + this.author +
                ",pubDate=" + this.pubDate +
                ",price=" + this.price +
                ",amount=" + this.amount +
                ")";
    }
}
